const friendLinkModel = require('@/models/friend_link.model')
const Service = require('./index')

class FriendLinkService extends Service {
  async findAll () {
    try {
      const data = await friendLinkModel.find()
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  async insertFriendLink (attrs) {
    const { name, link, logo } = attrs
    try {
      await friendLinkModel.create({ name, link, logo })
    } catch (error) {
      throw new Error(error)
    }
  }

  async updateFriendLink (id, attrs) {
    if (this.isNull(id)) {
      return {
        errorMessage: '缺少必要标识id'
      }
    }
    try {
      const exist = await friendLinkModel.exist(id)
      if (!exist) {
        return {
          errorMessage: '友情链接不存在'
        }
      }
      await friendLinkModel.update(id, attrs)
    } catch (error) {
      throw new Error(error)
    }
  }

  async deleteFriendLinks (ids) {
    if (!ids?.length) {
      return {
        errorMessage: '必要标识ids不能为空'
      }
    }
    try {
      await friendLinkModel.delete(ids)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new FriendLinkService()
